//
//  LBMEmoticonPackage.swift
//  EmoticonKeyboard
//
//  Created by 刘保满 on 16/5/30.
//  Copyright © 2016年 itheima. All rights reserved.
// 表情的分组模型

import UIKit

//每页显示的表情数据
let EmoticonPageCount = 21
let EmoticonColCount = 7
let EmoticonRowCount = 3
let LBMEmoticonBtnDidClick = "LBMEmoticonBtnDidClick"


class LBMEmoticonPackage: NSObject {

    var name: String?
    lazy var sectionEmoticon = [[LBMEmoticon]]()
    
    init(id: String, name: String, array: [[String: String]]) {
        self.name = name
        super.init()
        
        var tempArray = [LBMEmoticon]()
        
        var count = 0
        //遍历字典类型的数组,字典转模型
        for item in array {
            //字典转模型
            let e = LBMEmoticon(id: id, dict: item)
            tempArray.append(e)
            count+=1
            //每隔20个表情添加一个删除的模型
            if count == 20 {
                //添加一个删除按钮对应的模型
                let delete = LBMEmoticon(isDelete: true)
                tempArray.append(delete)
                count = 0
                
            }
            
        }
        
        
        let delta = tempArray.count % 21
        //需要添加空白表情的时候才进入
        if delta > 0 {
            for _ in delta..<20 {
                
                //添加空白模型
                let empty = LBMEmoticon(isEmpty: true)
                tempArray.append(empty)
       
            }
            //在空白表情之后还要在最后一个添加一个删除模型
            let delete  = LBMEmoticon(isDelete: true)
            tempArray.append(delete)
        }
        sectionEmoticon = sectionEmoticons(tempArray)
      }
    
    //将[LBMEmoticon] 处理成[[LBMEmoticon]]
    private func sectionEmoticons(array: [LBMEmoticon]) -> [[LBMEmoticon]] {
        let pageCount = (array.count - 1) / EmoticonPageCount +  1
        var sectionArr = [[LBMEmoticon]]()
        for i in 0..<pageCount {
            
            let loc = i * EmoticonPageCount
            var length = EmoticonPageCount
            if loc + length > array.count {
                length = array.count - loc
            }
            let subArray = (array as NSArray).subarrayWithRange(NSRange(location: loc, length: length))
            sectionArr.append(subArray as! [LBMEmoticon])
        }
        return sectionArr
    
    }

    
}
